package m2acsis.algebre.helpers;

import java.util.List;

import m2acsis.algebre.ElemAnneau;
import m2acsis.algebre.ElemGroupe;

public class ListHelper {

	@SuppressWarnings("unchecked")
	public static <T> T[][] getArray(List<List<T>> list){
		
		T[][] elements = null;
		int size = list.size();
		
		if(isElemAnneau(list)){
		
			elements = (T[][]) new ElemAnneau[size][size];
		
			for(int i = 0; i < size; i++)
				elements[i] = (T[]) list.get(i).toArray(new ElemAnneau[]{});
		}
		else{
			
			elements = (T[][]) new ElemGroupe[size][size];
			
			for(int i = 0; i < size; i++)
				elements[i] = (T[]) list.get(i).toArray(new ElemGroupe[]{});
		}
		
		return elements;
	}
	
	private static <T> boolean isElemAnneau(List<List<T>> list){
		
		if(list != null){
		
			for(int i = 0; i < list.size(); i++){
				
				List<T> ol = list.get(i);
				if(ol.size() > 0){
					if(ol.get(0) instanceof ElemAnneau)
						return true;
					else
						return false;
				}
			}
		}
		
		return false;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T[] getArray(List<?> list){
		
		if(list != null && list.size() > 0){
			
			Object o = list.get(0);

			//ElemAnneau en premier sinon on aura toujours ElemGroupe ;)
			if(o instanceof ElemAnneau)
				return (T[]) list.toArray(new ElemAnneau[]{});
			else if(o instanceof ElemGroupe)
				return (T[]) list.toArray(new ElemGroupe[]{});
		}
		
		return null;
	}
}
